﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhuyenMaiVang.DAL;
using KhuyenMaiVang.Helpers;

namespace KhuyenMaiVang.Areas.Admin.Controllers
{ 
    public class VoucherController : Controller
    {
        private KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();

        //
        // GET: /Admin/Voucher/

        public ViewResult Index()
        {
            ViewBag.TempDataSuccess = (String)TempData["success"];
            var vouchers = db.Vouchers.Include("Category").Include("City").Include("Business");
            return View(vouchers.ToList());
        }

        //
        // GET: /Admin/Voucher/Details/5

        public ViewResult Details(int id)
        {
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            return View(voucher);
        }

        //
        // GET: /Admin/Voucher/Create

        public ActionResult Create()
        {
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name");
            ViewBag.CityID = new SelectList(db.Cities, "ID", "Name");
            ViewBag.BusinessID = new SelectList(db.Businesses, "ID", "MangerName");
            return View();
        } 

        //
        // POST: /Admin/Voucher/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Voucher voucher)
        {
            if (ModelState.IsValid)
            {
                
                if (!ProcessHelper.checkExistVoucher(0, voucher.URLAlias))
                {
                    voucher.VoucherStatusID = 1;
                    voucher.CreatedTime = DateTime.Now;
                    db.Vouchers.AddObject(voucher);
                    db.SaveChanges();
                    TempData["success"] = "Đã thêm voucher:" + voucher.Title;
                    return RedirectToAction("Index");  
                }
                else
                {
                    ModelState.AddModelError("", "Đường dẫn voucher này đã tồn tại");
                }
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", voucher.CategoryID);
            ViewBag.CityID = new SelectList(db.Cities, "ID", "Name", voucher.CityID);
            ViewBag.BusinessID = new SelectList(db.Businesses, "ID", "MangerName");
            return View(voucher);
        }
        
        //
        // GET: /Admin/Voucher/Edit/5
 
        public ActionResult Edit(int id)
        {
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", voucher.CategoryID);
            ViewBag.CityID = new SelectList(db.Cities, "ID", "Name", voucher.CityID);
            ViewBag.BusinessID = new SelectList(db.Businesses, "ID", "MangerName", voucher.BusinessID);
            return View(voucher);
        }

        //
        // POST: /Admin/Voucher/Edit/5

        [HttpPost]
        public ActionResult Edit(Voucher voucher)
        {
            if (ModelState.IsValid)
            {
                if (!ProcessHelper.checkExistVoucher(voucher.ID, voucher.URLAlias))
                {
                    db.Vouchers.Attach(voucher);
                    db.ObjectStateManager.ChangeObjectState(voucher, EntityState.Modified);
                    db.SaveChanges();
                    TempData["success"] = "Đã sửa voucher:" + voucher.Title;
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Đường dẫn voucher này đã tồn tại");
                }
            }
            ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", voucher.CategoryID);
            ViewBag.CityID = new SelectList(db.Cities, "ID", "Name", voucher.CityID);
            ViewBag.BusinessID = new SelectList(db.Businesses, "ID", "MangerName", voucher.BusinessID);
            return View(voucher);
        }

        //
        // GET: /Admin/Voucher/Delete/5
 
        public ActionResult Delete(int id)
        {
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            return View(voucher);
        }

        //
        // POST: /Admin/Voucher/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            db.Vouchers.DeleteObject(voucher);
            db.SaveChanges();
            TempData["success"] = "Đã xóa voucher:" + voucher.Title;
            return RedirectToAction("Index");
        }

        //
        // GET: /Admin/Voucher/Change/5

        public ActionResult Change(int id)
        {
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            return View(voucher);
        }

        //
        // POST: /Admin/Voucher/Delete/5

        [HttpPost, ActionName("Change")]
        public ActionResult ChangeConfirmed(int id)
        {
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            if (voucher.VoucherStatusID == 1)
            {
                voucher.VoucherStatusID = 0;
            }
            else
            {
                voucher.VoucherStatusID = 1;
            }
            db.SaveChanges();
            TempData["success"] = "Đã thay đổi trạng thái voucher:" + voucher.Title;
            return RedirectToAction("Index");
        }
        //
        // GET: /Admin/Voucher/Address/5

        public ActionResult AddAddress(int id)
        {
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            var lstAddress = db.Addresses.Where(m => m.BusinessID == voucher.BusinessID);
            ViewBag.VoucherID = new SelectList(db.Vouchers.Where(m => m.ID == id), "ID", "Title", id);
            ViewBag.AddressID = new SelectList(lstAddress, "ID", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult AddAddress(AddressVouCher addressvoucher)
        {
            if (ModelState.IsValid)
            {
                db.AddressVouChers.AddObject(addressvoucher);
                db.SaveChanges();
                return RedirectToAction("Address", new { id = addressvoucher.VoucherID});
            }

            ViewBag.VoucherID = new SelectList(db.Vouchers, "ID", "Title", addressvoucher.VoucherID);
            ViewBag.AddressID = new SelectList(db.Addresses, "ID", "Name", addressvoucher.AddressID);
            return View(addressvoucher);
        }
        //
        // GET: /Admin/AddressVoucher/Delete/5

        public ActionResult DeleteAddress(int id)
        {
            AddressVouCher addressvoucher = db.AddressVouChers.Single(a => a.ID == id);
            return View(addressvoucher);
        }

        //
        // POST: /Admin/AddressVoucher/Delete/5

        [HttpPost, ActionName("DeleteAddress")]
        public ActionResult DeleteAddressConfirmed(int id)
        {
            AddressVouCher addressvoucher = db.AddressVouChers.Single(a => a.ID == id);
            db.AddressVouChers.DeleteObject(addressvoucher);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        //A
        // GET: /Admin/Voucher/AddAddress/5

        public ActionResult Address(int id)
        {
            Voucher voucher = db.Vouchers.Single(v => v.ID == id);
            List<AddressVouCher> lstAddressVoucher = db.AddressVouChers.Where(m => m.VoucherID == voucher.ID).ToList();
            ViewBag.Voucher = voucher;
            return View(lstAddressVoucher);
        }

        //Picture
        
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}